import {defineStore} from 'pinia'
import {reqLogin} from "@/api/user/user.js";
import type {loginForm,loginRes} from '@/api/user/type.ts'
import {useRouter} from 'vue-router'
import {UserState } from "./types.js"
import {SET_Token,GET_Token} from "@/utils/token.js";
const router = useRouter()
let useUserStore = defineStore('User',{
    state: ():UserState => {
        return {
            token:GET_Token()
        }
    },
    actions:{
        async login (data:loginForm) {
            const res:loginRes = await  reqLogin(data)
            console.log('login', res)
            if (res.code === 200) {
                SET_Token(res.data.token as string)
                localStorage.setItem('token', (res.data.token as string))
                return 'ok'
            }
            else {
                return Promise.reject(new Error(res.data.message))
            }
        }
    },
    getters:{

    }
})

export default useUserStore